<template>
    <div>
        <span class="title">系统设置</span>
        <span style="float:right;height:30px;">
            <Button type="warning" @click="handleSetting">告警阈值设置</Button>
        </span>
        <Modal
            :title="settingTitle"
            v-model="settingModal"
            :loading = "settingloading"
            @on-ok="confirmSetting"
            class-name="vertical-center-modal">

            <Form ref="formInline" style="margin-top:15px;" :label-width="200">
                <FormItem label="全局设备环境温差阈值">
                    <InputNumber :min="0.0" v-model="settingData.data_1" style="width:50%;"></InputNumber>
                    <Poptip
                        confirm
                        title="确定全局设置当前内容吗？"
                        @on-ok="gloablsetting">
                        <Button type="success">全局重置</Button>
                    </Poptip>
                </FormItem>
                <FormItem label="历史数据统计天数">
                    <InputNumber :min="0.0" v-model="settingData.data_2" style="width:50%;"></InputNumber>
                </FormItem>
                <FormItem label="历史温度均值阈值">
                    <InputNumber :min="0.0" v-model="settingData.data_3" style="width:50%;"></InputNumber>
                </FormItem>
                <FormItem label="紧急温升阈值">
                    <InputNumber :min="0.0" v-model="settingData.data_4" style="width:50%;"></InputNumber>
                </FormItem>
            </Form> 
        </Modal>

        <Tabs :value="tabTitle[0]" @on-click = "handleToggle">
            <TabPane :label="item" :name="item" v-for="(item,key) in tabTitle" :key="key">
                <p class="btn_group">
                    <span class="admintitle" style="float:left;">{{admintitle}}</span>
                    <Button type="info" @click="handleadd" :disabled='(admintitle=="设备列表管理" || admintitle=="网关列表管理")?true:false'>新增</Button>
                    <Button type="success" @click="handlereset" :disabled='resetdisabled'>修改</Button>
                    <Button type="warning" @click="handledelete" :disabled='deletedisabled'>删除</Button>
                </p>
                <Table :columns="columns" ref="selection" :data="data" style="margin-bottom:15px;" @on-select = "selectedItem" @on-selection-change="selectedItemchange"></Table>
                <Page :total="total" :current="currenPage" @on-change="changePage" style="text-align:right;margin-top:10px;" show-elevator></Page>
            </TabPane>
        </Tabs>

        <Modal
            :title="Title"
            v-model="modal_value"
            :loading = "loading"
            @on-ok="confirm"
            :mask-closable="false">
            <Form :label-width="80" v-if="admintitle==='变电站管理'">
                <FormItem label="变电站编号">
                    <Input v-model="transformer_substation_add.code" :disabled='transformer_substation_add.codedisbled' placeholder="输入变电站编号"></Input>
                </FormItem>
                <FormItem label="名称">
                    <Input v-model="transformer_substation_add.name" placeholder="输入名称" :disabled='transformer_substation_add.namedisabled'></Input>
                </FormItem>
                <FormItem label="地址">
                    <Input v-model="transformer_substation_add.address" placeholder="输入地址"></Input>
                </FormItem>
                <FormItem label="负责人">
                    <Input v-model="transformer_substation_add.charge_man" placeholder="输入负责人"></Input>
                </FormItem>
                <FormItem label="联系电话">
                    <Input v-model="transformer_substation_add.phone" placeholder="输入联系电话"></Input>
                </FormItem>
                <FormItem label="通知邮箱">
                    <Input v-model="transformer_substation_add.email" placeholder="输入邮箱"></Input>
                </FormItem>
            </Form>

            <Form :label-width="100" v-if="admintitle==='设备类别管理'">
                <FormItem label="设备类别名称">
                    <Input v-model="devtype_info.name" placeholder="输入设备类别名称"></Input>
                </FormItem>
                <FormItem label="设备类别编号">
                    <Input v-model="devtype_info.code" placeholder="输入设备类别编号" :disabled='devtype_info.codedisabled'></Input>
                </FormItem>
            </Form>

            <Form :label-width="100" v-if="admintitle==='设备编组管理'">
                <FormItem label="变电站名称">
                    <Select v-model="devgroup_info.codtransformer_substatione">
                        <Option v-for="(item,key) in tsnamelist" :value="item.name" :key = 'key'>{{item.name}}</Option>
                    </Select>
                </FormItem>
                <FormItem label="编组名称">
                    <Input v-model="devgroup_info.group_name" placeholder="输入编组名称"></Input>
                </FormItem>
                <FormItem label="位置">
                    <Input v-model="devgroup_info.group_location" placeholder="输入位置"></Input>
                </FormItem>
            </Form>

            <Form :label-width="100" v-if="admintitle==='设备列表管理'">
                <FormItem label="变电站">
                    <Select v-model="devlistinfo.codtransformer_substatione_name" :disabled="devlistinfo.codtransformer_substatione_name_dis">
                        <Option v-for="(item,key) in tsnamelist" :value="item.name" :key = 'key'>{{item.name}}</Option>
                    </Select>
                    
                </FormItem>
                <FormItem label="设备编组">
                    <Select v-model="devlistinfo.group_name" :disabled='true'>
                        <Option v-for="(item,key) in grouplist" :value="item.name" :key = 'key'>{{item.name}}</Option>
                    </Select>
                </FormItem>
                <FormItem label="设备类别">
                    <Input v-model="devlistinfo.devtype" placeholder="输入设备类别" :disabled='devlistinfo.devtype_dis'></Input>
                </FormItem>
                <FormItem label="设备名称">
                    <Input v-model="devlistinfo.devname" placeholder="输入设备名称"></Input>
                </FormItem>
                <FormItem label="设备编号">
                    <Input v-model="devlistinfo.devcode" placeholder="输入设备编号" :disabled='devlistinfo.devcode_dis'></Input>
                </FormItem>
                <FormItem label="绑定测温标签">
                    <Input v-model="devlistinfo.tag" placeholder="绑定测温标签" :disabled='devlistinfo.tag_dis'></Input>
                </FormItem>
                <FormItem label="报警温度（℃）">
                    <InputNumber :min="1" v-model="devlistinfo.temperature_warning"></InputNumber>
                    <!-- <Input v-model="devlistinfo.temperature_warning" placeholder="绑定测温标签"></Input> -->
                </FormItem>
            </Form>

            <Form :label-width="100" v-if="admintitle==='网关列表管理'">
                <FormItem label="变电站">
                    <Select v-model="gatewayinfo.codtransformer_substatione_name" disabled>
                        <Option v-for="(item,key) in tsnamelist" :value="item.name" :key = 'key'>{{item.name}}</Option>
                    </Select>
                </FormItem>
                <FormItem label="网关名称">
                    <Input v-model="gatewayinfo.gatewayname" placeholder="输入网关名称"></Input>
                </FormItem>
                <FormItem label="网关编号">
                    <Input v-model="gatewayinfo.gatewaycode" placeholder="请输入网关编号"></Input>
                </FormItem>
                <FormItem label="硬件id">
                    <Input v-model="gatewayinfo.gatewayid" placeholder="请输入硬件id" disabled></Input>
                </FormItem>
            </Form>
        </Modal>
        <Modal title="图片查看" v-model="visible">
            <img :src="deviceimg" style="width: 100%">
        </Modal>
    </div>
</template>
<script>
import util from '../../util/util'
export default {
    data(){
        return {
            settingTitle:'设置',
            settingModal:false,
            settingloading:true,
            settingData:{
                data_1:0,
                data_2:0,
                data_3:0,
                data_4:0
            },

            deviceimg:'',
            visible:false,
            currenPage:1,
            total:20,
            currentMode:'变电站',
            Title:'修改',
            modal_value:false,
            loading:true,
            currentRow:{},
            tsnamelist:[],
            grouplist:[],
            gatewayinfo:{
                codtransformer_substatione_name:'',
                gatewaycode:'',
                gatewayname:'',
                gatewayid:''
            },
            devtype_info:{
                code:'',
                name:'',
                codedisabled:false
            },
            transnamelist:['珠海变电站1','测试变电站2'],
            devtypelist:["类型1",'类型2'],
            devgroup_info:{
                codtransformer_substatione:'',
                group_name:'',
                group_location:''
            },

            devlistinfo:{
                codtransformer_substatione_name:'',
                devtype:'',
                devname:'',
                devcode:'',
                group_name:'',
                tag:'',
                devele:'',
                temperature_warning:0,
                codtransformer_substatione_name_dis:true,
                devtype_dis:true,
                devcode_dis:true,
                tag_dis:true
            },
            
            transformer_substation_add:{
                id:'',
                code:'',
                name:'',
                address:'',
                charge_man:'',
                phone:'',
                email:'',
                codedisbled:false,
                namedisabled:false
            },

            deletedisabled:true,
            resetdisabled:true,
            admintitle:'变电站管理',
            tabTitle:['变电站','设备类型','设备编组','设备列表','网关列表'],
            columns:[],
            transformer_substation_columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },{
                    title: '编号',
                    type:'index',
                    sortable:true
                },{
                    title: '变电站编号',
                    key: 'code',
                    sortable: true
                },{
                    title: '名称',
                    key: 'name',
                    sortable: true
                },{
                    title:'负责人',
                    key:'charge_man',
                    sortable:true
                },{
                    title:'地址',
                    key:'address',
                    sortable:true
                },{
                    title:'联系电话',
                    key:'phone',
                    sortable:true
                },{
                    title:'通知邮箱',
                    key:'email',
                    sortable:true
                }
            ],
            transformer_substation_data:[
              
            ],
            devtype_columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },{
                    title: '编号',
                    type:'index',
                    sortable:true
                },
                {
                    title: '设备类型名称',
                    key: 'devtypename',
                    sortable: true
                },{
                    title: '设备类型编号',
                    key: 'devtypecode',
                    sortable: true
                }
            ],
            devtypedata:[],
            devgroup_columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },{
                    title: '编号',
                    type:'index',
                    sortable:true
                },
                {
                    title: '变电站',
                    key: 'codtransformer_substatione',
                    sortable: true
                },{
                    title: '编组名称',
                    key: 'group_name',
                    sortable: true
                },{
                    title:'位置',
                    key:'group_location',
                    sortable:true
                }
            ],
            devgroup_data:[],
            devlist_columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '编号',
                    type:'index',
                    sortable:true
                },{
                    title:'设备编号',
                    key:'devcode',
                    sortable:true
                },{
                    title:'设备名称',
                    key:'devname',
                    sortable:true
                },{
                    title: '设备类别',
                    key: 'devtype',
                    sortable: true
                },{
                    title: '变电站名称',
                    key: 'codtransformer_substatione_name',
                    sortable: true
                },{
                    title:'设备编组',
                    key:'group_name',
                    sortable:true
                },{
                    title:'绑定标签',
                    key:'tag',
                    sortable:true
                },{
                    title:'设备电量',
                    key:'devele',
                    sortable:true
                },{
                    title:'报警温度值',
                    key:'temperature_warning',
                    sortable:true
                },{
                    title:'设备图片',
                    render:(h,params)=>{
                        let vm = this;
                        return h('div',[
                            h('Button',{
                                props: {
                                    type: 'primary'
                                },
                                on:{
                                    click:()=>{
                                        vm.deviceimg = params.row.pic_path;
                                        vm.visible = true;
                                    }
                                }
                            },'查看图片')
                        ])
                    }
                },{
                    title:'操作',
                    render:(h,params)=>{
                        let text_1 = params.row.elewarningstatus===1?'关闭电量预警':'开启电量预警';
                        let text_2 = params.row.temperaturewarningstatus===1?'关闭温度预警':'开启温度预警';
                        let bac_1 = params.row.elewarningstatus===1?'#19a7f0':'#dd514c';
                        let bac_2 = params.row.temperaturewarningstatus===1?'#19a7f0':'#dd514c';
                        return h('div',{
                            style:{
                                padding:'5px'
                            }
                        },[
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                    background:bac_1,
                                    borderColor:bac_1,
                                    marginBottom:'10px'
                                },
                                on: {
                                    click: () => {
                                        if(params.row.elewarningstatus===1){
                                            params.row.elewarningstatus=0;
                                            text_1 = '开启电量预警';
                                            this.open_close_warning(params.row.id,params.row.temperaturewarningstatus,params.row.elewarningstatus,'close','battle');
                                        }else{
                                            params.row.elewarningstatus=1;
                                            text_1 = '关闭电量预警';
                                            this.open_close_warning(params.row.id,params.row.temperaturewarningstatus,params.row.elewarningstatus,'open','battle');
                                        }
                                    }
                                }
                            }, text_1),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                    background:bac_2,
                                    borderColor:bac_2,
                                },
                                on: {
                                    click: () => {
                                        if(params.row.temperaturewarningstatus===1){
                                            params.row.temperaturewarningstatus=0;
                                            text_2 = '开启温度预警';
                                            this.open_close_warning(params.row.id,params.row.temperaturewarningstatus,params.row.elewarningstatus,'close','temperature');
                                        }else{
                                            params.row.temperaturewarningstatus=1;
                                            text_2 = '关闭温度预警';
                                            this.open_close_warning(params.row.id,params.row.temperaturewarningstatus,params.row.elewarningstatus,'open','temperature');
                                        }
                                    }
                                }
                            }, text_2),
                        ])
                    }
                }
            ],
            devlist_data:[],
            data:[],
            gateway_columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '编号',
                    type:'index',
                    sortable:true
                },
                {
                    title:'网关名称',
                    key:'gatewayname',
                    sortable:true
                },
                {
                    title:'网关编号',
                    key:'gatewaycode',
                    sortable:true
                },{
                    title:'变电站',
                    key:'tsname',
                    sortable:true
                },{
                    title:'网关编号',
                    key:'gatewaycode',
                    sortable:true
                },
                {
                    title:'在线状态',
                    render:(h,params) => {
                        let coltext = 'green';
                        let text = '在线';
                        if(params.row.onlinestatus===1){
                            coltext = 'green';
                            text = '在线';
                        }else{
                            coltext = 'red';
                            text = '离线';
                        }
                        return h('div',[
                            h('span',{
                                style:{
                                    color:coltext
                                }
                            },text)
                        ])
                    }
                },{
                    title:'最后修改日期',
                    key:'last_mod_date',
                    sortable:true
                },{
                    title:'最后修改用户',
                    key:'last_mod_user',
                    sortable:true
                },
            ],
            gateway_data:[]
        }
    },
    created(){
        this.columns = this.transformer_substation_columns;
        this.data = this.transformer_substation_data;
        this.GetSubstation(1,10);
        //this.GetDevGroup(1,10);
        this.get_Ts_Type_Group();
        this.getSettingData();
    },
    methods:{
        confirmSetting:function(){
            this.settingModal = false;
            this.submitsettingData()
        },
        handleSetting:function(){
            this.settingModal = true;
        },
        changeSettingLoading() {
            this.settingloading = false;
            this.$nextTick(() => {
                this.settingloading = true;
            });
        },
        gloablsetting:function(){
            let vm = this;
            util.ajax({
                method: 'PUT',
                url: '/util-api-reset-device-top/',
                data:{
                    reset_top:vm.settingData.data_1
                }
            })
            .then(function (response) {
                switch(response.status){
                    case 200:
                        vm.$Message.success("重置成功");
                        break;
                    
                }
            })
            .catch(function(err){
                switch(err.response.status){
                    case 403:
                        vm.$Message.info("权限不足");
                        break;
                }
            })
        },
        submitsettingData:function(postdata){
            let vm = this;
            util.ajax({
                method: 'PATCH',
                url: '/api-global-options/1/',
                data:{
                    global_warning_history_count_hourly_avg_temp:vm.settingData.data_1,
                    global_warning_history_count_hourly_days:vm.settingData.data_2,
                    global_warning_top_env_temp_differ:vm.settingData.data_3,
                    global_warning_urgent_temp_rise:vm.settingData.data_4
                }
            })
            .then(function (response) {
                if(response.status==200){
                    vm.$Message.success('保存设置成功');
                }else{
                    vm.$Message.success('保存设置失败');
                    return vm.changeSettingLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        getSettingData:function(){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/api-global-options/'
            })
            .then(function (response) {
                if(response.status==200){
                    vm.settingData={
                        data_1:Number(response.data.results[0].global_warning_history_count_hourly_avg_temp),
                        data_2:Number(response.data.results[0].global_warning_history_count_hourly_days),
                        data_3:Number(response.data.results[0].global_warning_top_env_temp_differ),
                        data_4:Number(response.data.results[0].global_warning_urgent_temp_rise)
                    }
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },


        get_Ts_Type_Group:function(){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/util-api-sgdlist/'
            })
            .then(function (response) {
                if(response.status==200){
                    vm.tsnamelist = [];
                    vm.grouplist = [];
                    for(let i = 0;i<response.data.substation.length;i++){
                        vm.tsnamelist.push(response.data.substation[i]);
                    }
                    for(let i = 0;i<response.data.device_group.length;i++){
                        vm.grouplist.push(response.data.device_group[i]);
                    }
                    
                }else{
                    vm.$Message.error('获取变电站列表失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        changePage:function(Page){
            if(this.admintitle=='变电站管理'){
                this.GetSubstation(Page,10);
            }else if(this.admintitle=='设备类别管理'){
                this.GetDevType(Page,10);
            }else if(this.admintitle=='设备编组管理'){
                this.GetDevGroup(Page,10);
            }else if(this.admintitle=='设备列表管理'){
                this.GetDeviceList(Page,10);
            }else if(this.admintitle=='网关列表管理'){
                this.GetGatewayList(Page,10);
            }
        },
        checkoutphone:function(phoneNumber){
            let reg=11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
            if(phoneNumber==''){
                return false
            }else if(!reg.test(phoneNumber)){
                return false
            }
            return true;
        },
        checkemail:function(email){
　　        let reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); 
            if(email===''){
                return false;
            }else if(!reg.test(email)){
                return false
            }
            return true;
        },
        handleToggle:function(name){
            for(let i = 0;i<this.$refs.selection.length;i++){
                this.$refs.selection[i].selectAll(false);
            }
            if(name==='变电站'){
                this.admintitle = '变电站管理';
                this.columns = this.transformer_substation_columns;
                this.data = this.transformer_substation_data;
                this.GetSubstation(1,10);
            }else if(name==='设备类型'){
                this.admintitle = '设备类别管理';
                this.data = this.devtypedata;
                this.columns = this.devtype_columns;
                this.GetDevType(1,10);
            }else if(name==='设备编组'){
                this.admintitle = '设备编组管理';
                this.columns = this.devgroup_columns;
                this.data = this.devgroup_data;
                this.GetDevGroup(1,10);
            }else if(name==='设备列表'){
               // console.log(this.grouplist)
                //console.log(this.tsnamelist)
                this.admintitle = '设备列表管理';
                this.columns = this.devlist_columns;
                this.data = this.devlist_data;
                this.GetDeviceList(1,10);
            }else if(name==='网关列表'){
                this.admintitle = '网关列表管理';
                this.columns = this.gateway_columns;
                this.data = this.gateway_data;
                this.GetGatewayList(1,10);
            }
        },
        handleadd:function(){
            if(this.admintitle === '变电站管理'){
                this.transformer_substation_add = {
                    charge_man:'',
                    code:'',
                    name:'',
                    address:'',
                    phone:'',
                    email:'',
                    codedisbled:false,
                    namedisabled:false
                };
                this.Title = '新增变电站';
            }else if(this.admintitle === '设备类别管理'){
                this.devtype_info = {
                    name:'',
                    code:'',
                    codedisabled:false
                }
                this.Title = '新增设备类型';
            }else if(this.admintitle === '设备编组管理'){
                this.devgroup_info = {
                    codtransformer_substatione:'',
                    group_name:'',
                    group_location:''
                };
                this.Title = '新增设备编组';
            }else if(this.admintitle === '设备列表管理'){
                this.devlistinfo = {
                    codtransformer_substatione_name:'',
                    devtype:'',
                    devname:'',
                    devcode:'',
                    tag:'',
                    devele:'',
                    temperature_warning:'',
                    codtransformer_substatione_name_dis:true,
                    devtype_dis:true,
                    devcode_dis:true,
                    tag_dis:true
                };
                this.Title = '新增设备';
            }else if(this.admintitle === '网关列表管理'){
                this.Title = '新增网关';
            }
            this.modal_value = true;
        },
        handlereset:function(){
            if(this.admintitle === '变电站管理'){
                this.transformer_substation_add = {
                    code:this.currentRow.code,
                    name:this.currentRow.name,
                    address:this.currentRow.address,
                    phone:this.currentRow.phone,
                    charge_man:this.currentRow.charge_man,
                    email:this.currentRow.email,
                    codedisbled:true,
                    namedisabled:true
                };
                this.Title = '修改变电站信息';
            }else if(this.admintitle === '设备类别管理'){
                this.devtype_info = {
                    code:this.currentRow.devtypecode,
                    name:this.currentRow.devtypename,
                    codedisabled:true,
                };
                this.Title = '修改设备类型';
            }else if(this.admintitle === '设备编组管理'){
                this.devgroup_info = Object.assign(this.devgroup_info,this.currentRow);
                this.Title = '修改设备编组';
            }else if(this.admintitle === '设备列表管理'){
                //console.log(this.devlistinfo);
                //console.log(this.currentRow);
                this.devlistinfo = Object.assign(this.devlistinfo, this.currentRow);
                this.devlistinfo.temperature_warning = parseFloat(this.devlistinfo.temperature_warning.replace('℃',''));
                this.Title = '修改设备';
            }else if(this.admintitle === '网关列表管理'){
                
                this.gatewayinfo={
                    id:this.currentRow.id,
                    codtransformer_substatione_name:this.currentRow.tsname,
                    gatewaycode:this.currentRow.gatewaycode,
                    gatewayname:this.currentRow.gatewayname,
                    gatewayid:this.currentRow.uid
                }
                //console.log(this.gatewayinfo)
            }
            this.modal_value = true;
        },
        handledelete:function(){
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除当前选中信息吗？</p>',
                loading: true,
                onOk: () => {
                    if(this.admintitle==='变电站管理'){
                        this.$refs.selection[0].selectAll(false);
                        this.DeleteSubstation();
                    }else if(this.admintitle==='设备列表管理'){
                        this.$refs.selection[1].selectAll(false);
                        this.DeleteDevice();
                    }else if(this.admintitle==='设备类别管理'){
                        this.$refs.selection[2].selectAll(false);
                        this.DeleteDevType();
                    }else if(this.admintitle==='设备编组管理'){
                        this.$refs.selection[3].selectAll(false);
                        this.DeleteDevGroup();
                    }else if(this.admintitle==='网关列表管理'){
                        this.$refs.selection[4].selectAll(false);
                        this.Deletegateway();
                    }
                }
            });
        },
        selectedItem:function(selection,row){
            this.currentRow = row;
        },
        selectedItemchange:function(selection){
            if(selection.length==1){
                this.deletedisabled = false;
                this.resetdisabled = false;
            }else{
                this.deletedisabled = true;
                this.resetdisabled = true;
            }
        },
        changeLoading() {
            this.loading = false;
            this.$nextTick(() => {
                this.loading = true;
            });
        },
        confirm:function(){
            if(this.admintitle === '变电站管理'){
                let submitData = this.transformer_substation_add;
                delete submitData.codedisbled;
                delete submitData.namedisabled;
                for(let i in submitData){
                   
                    if(!submitData[i]){
                        this.$Message.error('输入信息有误,请检查');
                        return this.changeLoading();
                    }
                }
                if(!this.checkoutphone(submitData.phone)){
                    this.$Message.error("请输入正确的手机号码");
                    return this.changeLoading();
                }
                if(!this.checkemail(submitData.email)){
                    this.$Message.error("请输入正确的邮箱");
                    return this.changeLoading();
                }
                if(this.Title=='新增变电站'){
                    this.CreateSubstation(submitData);
                }
                if(this.Title=='修改变电站信息'){
                    this.UpdateSubstation(submitData);
                }
            }else if(this.admintitle === '设备类别管理'){
                let submitData = this.devtype_info;
                delete submitData.codedisabled;
                for(let i in submitData){
                    if(!submitData[i]){
                        this.$Message.error('输入信息有误,请检查');
                        return this.changeLoading();
                    }
                }
                if(this.Title==='新增设备类型'){
                    this.CreateDevType(submitData);
                }
                if(this.Title==='修改设备类型'){
                    this.ResetDevType(submitData);
                }
                
            }else if(this.admintitle === '设备编组管理'){
                let submitData = this.devgroup_info;
                //console.log(submitData)
                for(let i in submitData){
                    if(!submitData[i]){
                        this.$Message.error('输入信息有误,请检查');
                        return this.changeLoading();
                    }
                }
                if(this.Title==='新增设备编组'){
                    this.CreateDevGroup(submitData);
                }
                if(this.Title==='修改设备编组'){
                    this.ResetDevGroup(submitData);
                }
                
            }else if(this.admintitle === '设备列表管理'){
            
                let submitData = {
                    codtransformer_substatione_name:this.devlistinfo.codtransformer_substatione_name,
                    devcode:this.devlistinfo.devcode,
                    devele:this.devlistinfo.devele,
                    devname:this.devlistinfo.devname,
                    devtype:this.devlistinfo.devtype,
                    id:this.devlistinfo.id,
                    tag:this.devlistinfo.tag,
                    temperature_warning:this.devlistinfo.temperature_warning
                };
                for(let i in submitData){
                    if(!submitData[i]){
                        this.$Message.error('输入信息有误,请检查');
                        return this.changeLoading();
                    }
                }
                if(this.Title==='修改设备'){
                    this.ResetDevice(submitData);
                }
            }else if(this.admintitle === '网关列表管理'){
                let submitData = this.gatewayinfo;
                for(let i in submitData){
                    if(!submitData[i]){
                        this.$Message.error('输入信息有误,请检查');
                        return this.changeLoading();
                    }
                }
                this.ResetGateway(submitData);
            }
        },
        GetSubstation:function(page,pagesize){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/api-substation/?page='+page+'&page_size='+pagesize
            })
            .then(function (response) {
                if(response.status==200){
                    vm.total = response.data.count;
                    vm.data = [];
                    vm.currenPage = page;
                    for(let i = 0;i<response.data.results.length;i++){
                        let item = {
                            id:response.data.results[i].id,
                            code:response.data.results[i].tsid,
                            name:response.data.results[i].name,
                            charge_man:response.data.results[i].charge_man,
                            address:response.data.results[i].address,
                            phone:response.data.results[i].phone,
                            email:response.data.results[i].notice_mail
                        }
            
                        vm.data.push(item);
                    }
                }else{
                    vm.$Message.error('获取变电站列表失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        CreateSubstation:function(submitData){
            let vm = this;
            util.ajax({
                method: 'post',
                url: '/api-substation/',
                data:{
                    tsid:vm.transformer_substation_add.code,
                    name:vm.transformer_substation_add.name,
                    charge_man:vm.transformer_substation_add.charge_man,
                    address:vm.transformer_substation_add.address,
                    phone:vm.transformer_substation_add.phone,
                    notice_mail:vm.transformer_substation_add.email
                }
            })
            .then(function (response) {
                if(response.status==201){
                    vm.data.shift();
                    vm.data.push(submitData);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('新建变电站成功');
                    vm.get_Ts_Type_Group();
                }else{
                    vm.$Message.error('创建变电站失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('创建变电站失败');
                return vm.changeLoading();
            })
        },
        UpdateSubstation:function(submitData){
            let vm = this;
            util.ajax({
                method: 'PATCH',
                url: '/api-substation/'+this.currentRow.id+'/',
                data:{
                    tsid:vm.transformer_substation_add.code,
                    name:vm.transformer_substation_add.name,
                    charge_man:vm.transformer_substation_add.charge_man,
                    address:vm.transformer_substation_add.address,
                    phone:vm.transformer_substation_add.phone,
                    notice_mail:vm.transformer_substation_add.email
                }
            })
            .then(function (response) {
                if(response.status==200){
                    vm.currenPage = 1;
                    vm.GetSubstation(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('修改变电站信息成功');
                    vm.get_Ts_Type_Group();
                }else{
                    vm.$Message.error('修改变电站信息失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('修改变电站信息失败');
                return vm.changeLoading();
            })
        },
        DeleteSubstation:function(){
            let vm = this;
            util.ajax({
                method: 'DELETE',
                url: '/api-substation/'+this.currentRow.id+'/'
            })
            .then(function (response) {
                if(response.status==204){
                    vm.data.splice(vm.data.indexOf(vm.currentRow),1);
                    vm.$Modal.remove();
                    vm.currenPage = 1;
                    vm.GetSubstation(1,10);
                    vm.$Message.success('删除变电站成功');
                    vm.get_Ts_Type_Group();
                }else{
                    vm.$Message.error('删除变电站失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('删除变电站失败');
                return vm.changeLoading();
            })
        },
        GetDeviceList:function(page,pagesize){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/api-device/?page='+page+'&page_size='+pagesize
            })
            .then(function (response) {
                if(response.status==200){
                    vm.currenPage = page;
                    vm.total = response.data.count;
                    vm.data = [];
                    for(let i = 0;i<response.data.results.length;i++){
                        let item = {
                            id:response.data.results[i].id,
                            codtransformer_substatione_name:response.data.results[i].tsname,
                            devtype:response.data.results[i].tname,
                            devname:response.data.results[i].name,
                            devcode:response.data.results[i].did,
                            tag:response.data.results[i].uid,
                            devele:response.data.results[i].battery+'%',
                            temperature_warning:response.data.results[i].top_temp+'℃',
                            elewarningstatus:response.data.results[i].battery_msg_flag,
                            temperaturewarningstatus:response.data.results[i].warning_msg_flag,
                            group_name:response.data.results[i].group_name,
                            pic_path:response.data.results[i].pic_hd
                        }
                        vm.data.push(item);
                    }
                }else{
                    vm.$Message.error('获取设备列表失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        ResetDevice:function(submitData){
            let vm = this;
            util.ajax({
                method: 'PATCH',
                url: '/api-device/'+this.currentRow.id+'/',
                data:{
                    id:vm.currentRow.id,
                    did:vm.devlistinfo.devcode,
                    name:vm.devlistinfo.devname,
                    tsname:vm.devlistinfo.codtransformer_substatione_name,
                    top_temp:vm.devlistinfo.temperature_warning,
                    uid:vm.devlistinfo.tag
                }
            })
            .then(function (response) {
                if(response.status==200){
                    vm.currenPage = 1;
                    vm.GetDeviceList(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('修改设备信息成功');
                }else{
                    vm.$Message.error('修改设备信息失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('修改设备信息失败');
                return vm.changeLoading();
            })
        },
        open_close_warning:function(id,temp_warning_status,battle_warning_status,flag,type){
            let vm = this;
            util.ajax({
                method: 'PATCH',
                url: '/api-device/'+id+'/',
                data:{
                    warning_msg_flag:temp_warning_status,
                    battery_msg_flag:battle_warning_status
                }
            })
            .then(function (response) {
                if(response.status==200){
                    //vm.GetDeviceList(1,10);
                    if(flag==='open'){
                        vm.$Message.success('开启成功');
                    }else if(flag==='close'){
                        vm.$Message.success('关闭成功');
                    }
                }else{
                    if(flag==='open'){
                        vm.$Message.error('开启失败');
                    }else if(flag==='close'){
                        vm.$Message.error('关闭失败');
                    }
                }
            })
            .catch(function(err){
                if(flag==='open'){
                    vm.$Message.error('开启失败');
                }else if(flag==='close'){
                    vm.$Message.error('关闭失败');
                }
            })
        },
        DeleteDevice:function(){
            let vm = this;
            util.ajax({
                method: 'DELETE',
                url: '/api-device/'+this.currentRow.id+'/'
            })
            .then(function (response) {
                if(response.status==204){
                    vm.data.splice(vm.data.indexOf(vm.currentRow),1);
                    vm.$Modal.remove();
                    vm.currenPage = 1;
                    vm.GetDeviceList(1,10);
                    vm.$Message.success('删除变电站成功');
                }else{
                    vm.$Message.error('删除变电站失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('删除变电站失败');
                return vm.changeLoading();
            })
        },
        GetDevType:function(page,pagesize){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/api-device-type/?page='+page+'&page_size='+pagesize
            })
            .then(function (response) {
                if(response.status==200){
                    vm.total = response.data.count;
                    vm.data = [];
                    vm.currenPage = page;
                    for(let i = 0;i<response.data.results.length;i++){
                        let item = {
                            id:response.data.results[i].id,
                            devtypename:response.data.results[i].name,
                            devtypecode:response.data.results[i].tid
                        }
                        vm.data.push(item);
                    }
                }else{
                    vm.$Message.error('获取设备列表失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        CreateDevType:function(submitData){
            let vm = this;
            util.ajax({
                method: 'post',
                url: '/api-device-type/',
                data:{
                    tid:vm.devtype_info.code,
                    name:vm.devtype_info.name
                }
            })
            .then(function (response) {
               // console.log(response)
                if(response.status==201){
                    vm.currenPage = 1;
                    vm.GetDevType(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('新建设备类型成功');
                }else{
                    vm.$Message.error('创建设备类型失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('创建设备类型失败');
                return vm.changeLoading();
            })
        },
        ResetDevType:function(submitData){
            let vm = this;
           // console.log(vm.currentRow);
            util.ajax({
                method: 'PATCH',
                url: '/api-device-type/'+vm.currentRow.id+'/',
                data:{
                    tid:vm.devtype_info.code,
                    name:vm.devtype_info.name
                }
            })
            .then(function (response) {
                if(response.status==200){
                    vm.currenPage = 1;
                    vm.GetDevType(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('修改设备类型成功');
                }else{
                    vm.$Message.error('修改设备类型失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('修改设备类型失败');
                return vm.changeLoading();
            })
        },
        DeleteDevType:function(){
            let vm = this;
            util.ajax({
                method: 'DELETE',
                url: '/api-device-type/'+this.currentRow.id+'/'
            })
            .then(function (response) {
                if(response.status==204){
                    vm.data.splice(vm.data.indexOf(vm.currentRow),1);
                    vm.$Modal.remove();
                    vm.currenPage = 1;
                    vm.GetDevType(1,10);
                    vm.$Message.success('删除设备类型成功');
                }else{
                    vm.$Message.error('删除设备类型失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('删除设备类型失败');
                return vm.changeLoading();
            })
        },
        GetDevGroup:function(page,pagesize){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/api-group/?page='+page+'&page_size='+pagesize
            })
            .then(function (response) {
                if(response.status==200){
                    vm.total = response.data.count;
                    vm.data = [];
                    vm.currenPage = page;
                    for(let i = 0;i<response.data.results.length;i++){
                        let item = {
                            id:response.data.results[i].id,
                            codtransformer_substatione:response.data.results[i].tsname,
                            group_name:response.data.results[i].group_name,
                            group_location:response.data.results[i].location_no
                        }
                       
                        vm.data.push(item);
                    }
                }else{
                    vm.$Message.error('获取设备列表失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        CreateDevGroup:function(submitData){
            let vm = this;
            util.ajax({
                method: 'post',
                url: '/api-group/',
                data:{
                    group_name:vm.devgroup_info.group_name,
                    location_no:vm.devgroup_info.group_location,
                    tsname:vm.devgroup_info.codtransformer_substatione
                }
            })
            .then(function (response) {
               // console.log(response)
                if(response.status==201){
                    vm.currenPage = 1;
                    vm.GetDevGroup(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('新建设备编组成功');
                    vm.get_Ts_Type_Group();
                }else{
                    vm.$Message.error('创建设备编组失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('创建设备编组失败');
                return vm.changeLoading();
            })
        },
        ResetDevGroup:function(submitData){
            let vm = this;
           // console.log(vm.currentRow);
            util.ajax({
                method: 'PATCH',
                url: '/api-group/'+vm.currentRow.id+'/',
                data:{
                    id:vm.currentRow.id,
                    group_name:vm.devgroup_info.group_name,
                    location_no:vm.devgroup_info.group_location,
                    tsname:vm.devgroup_info.codtransformer_substatione
                }
            })
            .then(function (response) {
                if(response.status==200){
                    vm.currenPage = 1;
                    vm.GetDevGroup(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('修改设备编组成功');
                    vm.get_Ts_Type_Group();
                }else{
                    vm.$Message.error('修改设备编组失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('修改设备编组失败');
                return vm.changeLoading();
            })
        },
        DeleteDevGroup:function(){
            let vm = this;
            util.ajax({
                method: 'DELETE',
                url: '/api-group/'+this.currentRow.id+'/'
            })
            .then(function (response) {
                if(response.status==204){
                    vm.data.splice(vm.data.indexOf(vm.currentRow),1);
                    vm.$Modal.remove();
                    vm.currenPage = 1;
                    vm.GetDevGroup(1,10);
                    vm.$Message.success('删除设备编组成功');
                    vm.get_Ts_Type_Group();
                }else{
                    vm.$Message.error('删除设备编组失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('删除设备编组失败');
            })
        },
        GetGatewayList:function(page,pagesize){
            let vm = this;
            util.ajax({
                method: 'get',
                url: '/api-reader/?page='+page+'&page_size='+pagesize
            })
            .then(function (response) {
               // console.log(response)
                if(response.status==200){
                    console.log(response)
                    vm.total = response.data.count;
                    vm.data = [];
                    vm.currenPage = page;
                    for(let i = 0;i<response.data.results.length;i++){
                        let item = {
                            id:response.data.results[i].id,
                            gatewayname:response.data.results[i].name,
                            gatewaycode:response.data.results[i].rid,
                            tsname:response.data.results[i].tsname,
                            onlinestatus:response.data.results[i].is_online,
                            last_mod_date:response.data.results[i].last_mod_datetime,
                            last_mod_user:response.data.results[i].last_mod_user,
                            uid:response.data.results[i].uid
                        }
                        vm.data.push(item);
                    }
                }else{
                    vm.$Message.error('获取网关列表失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('网络错误');
            })
        },
        ResetGateway:function(submitData){
            let vm = this;
           // console.log(vm.gatewayinfo);
            util.ajax({
                method: 'PATCH',
                url: '/api-reader/'+vm.currentRow.id+'/',
                data:{
                    rid:vm.gatewayinfo.gatewaycode,
                    name:vm.gatewayinfo.gatewayname,
                    uid :vm.gatewayinfo.gatewayid,
                    tsname:vm.gatewayinfo.codtransformer_substatione_name
                }
            })
            .then(function (response) {
                if(response.status==200){
                    vm.currenPage = 1;
                    vm.GetGatewayList(1,10);
                    vm.changeLoading();
                    vm.modal_value=false;
                    vm.$Message.success('修改网关成功');
                }else{
                    vm.$Message.error('修改网关失败');
                    return vm.changeLoading();
                }
            })
            .catch(function(err){
                vm.$Message.error('修改网关失败');
                return vm.changeLoading();
            })
        },
        Deletegateway:function(){
            let vm = this;
            util.ajax({
                method: 'DELETE',
                url: '/api-reader/'+this.currentRow.id+'/'
            })
            .then(function (response) {
                if(response.status==204){
                    vm.data.splice(vm.data.indexOf(vm.currentRow),1);
                    vm.$Modal.remove();
                    vm.currenPage = 1;
                    vm.GetGatewayList(1,10);
                    vm.$Message.success('删除网关成功');
                }else{
                    vm.$Message.error('删除网关失败');
                }
            })
            .catch(function(err){
                vm.$Message.error('删除网关失败');
            })
        },
    }
}
</script>
<style scoped>
    .title{
        color: #0e90d2;
        font-size: 20px;
        font-weight: 700;
    }
    .admintitle{
        color: #333;
        font-size: 20px;
        font-weight: 700;
    }
    .btn_group{
        text-align: right;
        padding-bottom: 20px;
    }
</style>
